package ch.il07.converter.persistence.entities;

import java.util.LinkedList;
import java.util.List;

public class Measurand {
	
	private Integer measurandID;
	private String name;
	private String description;
	private Unit defaultUnit;
	private List<Unit> units = new LinkedList<Unit>();
	
	/**
	 * @value owner of the measurand. null if no owner
	 */
	private User owner;
	
	public Measurand(String name, Unit defaultUnit){
		this(name, defaultUnit, null);
	}
	
	/**
	 * Creates a new measurand.
	 * @param name Measurand name. Example: Strecke
	 * @param defaultUnit Base Unit. Example: Meter
	 */
	public Measurand(String name, Unit defaultUnit, User owner) {
		this.setName(name);
		this.setDefaultUnit(defaultUnit);
		this.setOwner(owner);
	}

	@Override
	public String toString() {
		return String
				.format("[MeasurandID=%d, Name=%s, Description=%s, DefaultUnit=%s, NumberOfUnits=%d, Owner=%s]",
						measurandID, name, description, defaultUnit, (units != null ? units.size() : 0), owner);
	}
	
	public void setMeasurandId(Integer measurandID) {
		this.measurandID = measurandID;
	}

	public Integer getMeasurandID() {
		return measurandID;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDescription() {
		return description;
	}

	public void setDefaultUnit(Unit defaultUnit) {
		this.defaultUnit = defaultUnit;
	}

	public Unit getDefaultUnit() {
		return defaultUnit;
	}

	public void setUnits(List<Unit> units) {
		this.units = units;
	}
	
	public void addUnit(Unit unit){
		for(Unit u : units) {
			if(u.getName().equals(unit.getName())) {
				return;
			}
		}
		this.units.add(unit);
	}

	public List<Unit> getUnits() {
		return units;
	}

	public void setOwner(User owner) {
		this.owner = owner;
	}

	public User getOwner() {
		return owner;
	}
}